package com.example.cscot;

import com.volcengine.ark.runtime.service.ArkService;

import android.os.Bundle;
import android.os.StrictMode;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.cscot.databinding.ActivityMainBinding;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {
    // 全局变量
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        WeiLe.mainActivity = this;  // always in first line
        super.onCreate(savedInstanceState);

        com.example.cscot.databinding.ActivityMainBinding binding;
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // BottomNavigationViews initialization
        // BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_normal, R.id.navigation_cot, R.id.navigation_dialog,
                // 用确实项目名字，其实相当于：navigation_normal, navigation_cot, navigation_debug
                R.id.navigation_studymap, R.id.navigation_me)
                .build();
        NavController navController = Navigation.findNavController(this,
                R.id.nav_host_fragment_activity_main);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(binding.navView, navController);

        // permission initialization
        LewayHelper.setMainPermissions(this);
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        WeiLe.gLogText += LewayHelper.logLine("MainActivity.getPermission done");
        WeiLe.gLogText += LewayHelper.logLine("os.arch=" + System.getProperty("os.arch"));
        WeiLe.gEnglishSteps = LewayHelper.readRawFile(WeiLe.mainActivity, "cot_steps");
        initContextGlobals();
    } // onCreate

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void showToast(final String toast) {
        this.runOnUiThread(() -> Toast.makeText(this, toast, Toast.LENGTH_SHORT).show());
    }

    // 没有必要放到WeiLe里面去，因为这是APP特定的，基本上不可能通用
    public void initContextGlobals() {
        //
        WeiLe.gHashMap = new HashMap<>();
        LewayHelper.initHashMap();
        //
        WeiLe.gCotLlm = new LewayChat("DB_Ark");
        WeiLe.gDialogLlm = new LewayChat("DB_Ark");
        WeiLe.gCotLlm.initChat("");
        WeiLe.gDialogLlm.initChat("");
        WeiLe.gCotLlm.prepareRole( WeiLe.gHashMap.get("_STR_RLA"), new RtCallback() {
            @Override public void onRightTime(String strResult) {
                // WeiLe.gLogText += LewayHelper.logLine(strResult);
            }
        });
        WeiLe.gDialogLlm.prepareRole( WeiLe.gHashMap.get("_STR_RLA"), new RtCallback() {
            @Override public void onRightTime(String strResult) {
                // WeiLe.gLogText += LewayHelper.logLine(strResult);
            }
        });
    } // >>> initContextGlobals
    //
} // >>> MainActivity
